package com.youi.third.shareclound;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.sec.sdk.SecClient;
import com.sec.sdk.bean.BaseResponse;
import com.sec.sdk.bean.RemitBatchRequestDTO;
import com.sec.sdk.bean.RemitDetailRequestDTO;
import com.sec.sdk.bean.RemitQueryRequestDTO;
import com.sec.sdk.constants.SecGatewayConstants;
import com.sec.sdk.exception.SecGatewayException;
import com.sec.sdk.utils.SecSignature;
import com.youi.util.DateUtil;

public class SharecloundUtil {

	//商户应用ID
    private static final String APPID = "101912191611";
    //商户编号
    private static final String MERCHANTNO="101912191611";
    //商户密钥
    private static final String MERPRIVATE="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCIYv5qaXXrzARO8qTTsZgZVswJtQAYgOzC0HRDfcAvwY3nQ5pC7gxpIoL9nAUYpVS6EcgIwbk6tZL9UsWAS55ufpXT7d2xBmowxfp1gtO6tpKdc+qjn+CAu6vNtHymN7zyA80E3q6cJEF9ShliaE/RpzI5ww34SElhQClVkH3ktKL57Z/R5mLGtfWdGES5mzaYkMqL3TI1ddvroPN8FCofKVhwcdCFQY7i5hjM241vqSUi2xLXhupHwDpFNBj2YTTn6pVdTSbES08uxOcR9OGKA72f5uL9B6i9t/aucWP9SJqc0AyqcEnIxRy876TdbKZkUhRaEPMn12VrqJu+u++FAgMBAAECggEAGUbPhAU7l5HeEouhxKIbYEbX1cy30dLDDm/3vDJoFfG3TO8YfeMEZc/BmyL5RtINW9bDzeupWpogPdbDR728BRpsxjRFa/OWZqeqfXDbsLT5gzjieaS9xN4DP0J9/4hA+lF+A7MJhWKqyH+dKMHQWTi1dEMUKbmr79wmTPdUfbg+4P0WUOXO8plrJPK759S445GIVz7Y//akqpiMuZvzKyFwgtLF+nD58nv/25l1JMwi3whG5hwwGuDv9PgrnyGvQw+h4L2mjgfK1KZ/3ds/OYfYAZ6K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uEyQZoEkBOoQQcLeTLXvyTSyvz4wLcqwmlvIhH37pFs+MLWb/ovZ3nbd1L15RNpGNR59pnUSwSKkjwsbgtFng/ptMWZwUkv11egs+2UjpsltHtaEkw/w2Ai0N8JZTqaFdCnqZGkht4CSVQKOMZrAeF4AD1Siwfk0PqsFGQeJh1/iMdEA7+6P/s2K+OZjT0833nsM5MSoPOHJO4Y3mA+95KFLt30icJGNXm2U/jN4ykD6HPG4X0G8J2g8SuhB86YIOeSQB53afArB2ayfievz5QIDAQAB";   
    //回调地址
//    private static final String NotifyUrl="http://47.113.102.74/MeiYiJiaHealthManage/checkShareclound";
  //回调地址
    private static final String NotifyUrl="https://mp.myjjk.com.cn/MeiYiJiaHealthManage/checkShareclound";
    
    
   /**
    * 云享代付工具 
    * @param custBatchNo 商户批次号
    * @param batchAmt	批次总金额，保留两位小数，与明细总金额对应
    * @param orderNo    提现订单号
    * @param name	持卡人姓名
    * @param bank	银行名称		
    * @param idNum	身份证号
    * @return
    * @throws SecGatewayException
    * @throws IOException
    */
   public static String shareCloundPay(String custBatchNo,String batchAmt,String orderNo,String name,String bank,String bankCardNo,String idNum) throws SecGatewayException, IOException {
	   SecClient secClient = new SecClient(SecGatewayConstants.SERVER_URL,"settle.remit.api.payment", APPID,MERPRIVATE,"001",DateUtil.simpdfyMdHms.format(new Date()),20000,20000);
       //业务参数构建
       RemitBatchRequestDTO remitBatchRequestDTO = new RemitBatchRequestDTO();
       //商户批次号
       remitBatchRequestDTO.setCustBatchNo(custBatchNo);
       //批次总笔数
       remitBatchRequestDTO.setBatchNum(1);
       //new BigDecimal 时请写string类型 防止精度丢失
       //批次总金额与明细金额对应
       remitBatchRequestDTO.setBatchAmt(new BigDecimal(batchAmt));
       //回调地址
       remitBatchRequestDTO.setServerCallbackUrl(NotifyUrl);
       List<RemitDetailRequestDTO> list = new ArrayList<RemitDetailRequestDTO>();
       RemitDetailRequestDTO detail=new RemitDetailRequestDTO();
       detail.setCustNo(APPID);
       //商户打款明细订单号
	   detail.setCustOrderNo(orderNo);
	   //付款金额保留两位小数
	   detail.setOrderAmt(new BigDecimal(batchAmt));
	   //收款银行卡户名
	   detail.setRecvCustName(name);
	   //收款人证件类型
	   detail.setRecvIdType("IDENTITY");
	   //收款人证件编号
	   detail.setRecvIdNo(idNum);
	   //收款银行名称
	   detail.setRecvBankName(bank);
	   //收款银行卡号
	   detail.setRecvCardNo(bankCardNo);
	   //付款备注
	   detail.setRemark("美宜佳健康佳选官提现");
	   list.add(detail);     
       remitBatchRequestDTO.setRemitDetailList(list);
       BaseResponse responseDTO = secClient.excute(remitBatchRequestDTO);
       return responseDTO.getResponse();
   }
   
   /**
    * 付款订单查询
    * @param custBatchNo
    * @return
    * @throws IOException 
    * @throws SecGatewayException 
    */
   public static String getPayResult(String custBatchNo) throws SecGatewayException, IOException {
	   SecClient secClient = new SecClient(SecGatewayConstants.SERVER_URL,"settle.remit.api.query", APPID,MERPRIVATE,"001",DateUtil.simpdfyMdHms.format(new Date()),20000,20000);
	   RemitQueryRequestDTO requestDTO = new RemitQueryRequestDTO();
       requestDTO.setCustBatchNo(custBatchNo);
       BaseResponse responseDTO = secClient.excute(requestDTO);
       if (requestDTO!=null) {
    	   return responseDTO.getResponse();
       }
	   return null;	   
   }
   
   
   /**
    * 回调验证签名
    * @param method
    * @param sign_type
    * @param sign
    * @param timestamp
    * @param version
    * @param merchant_request_no
    * @param biz_content_key
    * @return
    * @throws SecGatewayException
    */
   public static Boolean sharecloundCallback(String method,String sign_type,String sign,String timestamp,String version,String merchant_request_no,String biz_content_key) throws SecGatewayException{
	   Map<String, String> params = new HashMap<String, String>();
       params.put(SecGatewayConstants.METHOD,method);
       params.put(SecGatewayConstants.SIGN_TYPE,sign_type);
       params.put(SecGatewayConstants.SIGN,sign);
       params.put(SecGatewayConstants.TIMESTAMP,timestamp);
       params.put(SecGatewayConstants.VERSION,version);
       params.put(SecGatewayConstants.MERCHANT_REQUEST_NO,merchant_request_no);
       params.put(SecGatewayConstants.BIZ_CONTENT_KEY,biz_content_key);
       boolean flag= SecSignature.rsaCheckV2(params, SECPUBLICKEY,SecGatewayConstants.CHARSET_UTF8,SecGatewayConstants.SIGN_TYPE_RSA2);
       if (flag) {
		return true;
       }
       return false;	   
   }
   
   public static void main(String[] args) throws SecGatewayException, IOException {
	   String result=getPayResult("50000861584066420094");
	   System.out.println(result);
}
}
